package com.xinshujia.config;

import com.google.gson.GsonBuilder;
import com.xinshujia.adapter.DateAdapter;
import com.xinshujia.adapter.LocalDateAdapter;
import com.xinshujia.adapter.LocalDateTimeAdapter;
import com.xinshujia.adapter.TimestampAdapter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.GsonHttpMessageConverter;

import java.sql.Timestamp;
import java.time.LocalDate;
import java.util.Date;

/**
 * *************************************
 * *   @author: 野狼上校               **
 * *   @since: 2024-08-01 13:44       **
 * *   @version: 1.0                  **
 * *************************************
 * <p>
 *
 *     用Gson代替jackson 作为会局Json转换器
 */
@Configuration
public class GsonConfig {
 @Bean
 public GsonHttpMessageConverter gsonHttpMessageConverter() {
  GsonHttpMessageConverter gsonHttpMessageConverter = new GsonHttpMessageConverter();
  GsonBuilder gsonBuilder = new GsonBuilder();
  gsonBuilder.registerTypeAdapter(Date.class, new DateAdapter());
  gsonBuilder.registerTypeAdapter(LocalDate.class, new LocalDateAdapter());
  gsonBuilder.registerTypeAdapter(LocalDateTimeAdapter.class, new LocalDateTimeAdapter());
  gsonBuilder.registerTypeAdapter(Timestamp.class, new TimestampAdapter());
  gsonHttpMessageConverter.setGson(gsonBuilder.create());
  return gsonHttpMessageConverter;
 }
}